﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetalBase.Base;
using System.Diagnostics.Contracts;
using RmProto.Automata.IA;
using System.Diagnostics;
using NetalBase.Base.Attributes;

namespace RmProto.Automata.CRA
{
	public class ComputationalResource :
		AutomatonBase<ICRA, ICRAEventPoint, CRADataPoint>,
		ICRA, ICRAEventPoint
	{
		#region States
		readonly CRc CRc;
		readonly CRf CRf;
		readonly CRr CRr;
		readonly CRp CRp;
		readonly CRe CRe;
		#endregion

		#region ICRA Members
		public virtual void e_CR_cc()
		{
			s_s(CRf, CRc);
		}

		public virtual void e_CR_fc()
		{
			s_s(CRc, CRf);
		}

		public virtual void e_CR_init(IA.IA i)
		{
			s_s(CRc, CRr);
		}

		public virtual void e_CR_p()
		{
			s_s(CRr, CRp);
		}

		public virtual void e_CR_s()
		{
			throw new NotImplementedException();
		}

		public virtual void e_CR_r()
		{
			throw new NotImplementedException();
		}

		public virtual void e_CR_es()
		{
			s_s(CRe, CRc);
		}

		public event z_CR_fin z_CR_fin;
		public event z_CR_e z_CR_e;
		#endregion

		#region ICRAEventPoint Members
		[RaiseEventMethod]
		public virtual void _z_CR_fin(object result)
		{
			s_s(CRp, CRc);
		}

		[RaiseEventMethod]
		public virtual void _z_CR_e(string errorMessage)
		{
			s_s(CRp, CRe);
		}
		#endregion

		#region Public auxillary methods
		public ComputationalResource()
		{
			CRf = createInitFinalState<CRf>();
			CRc = createState<CRc>();
			CRr = createState<CRr>();
			CRp = createState<CRp>();
			CRe = createState<CRe>();

			z_CR_fin +=
				(msg) => DataPoint.Interpretation.e_I_fin(msg);

			z_CR_e +=
				(errorMsg) => DataPoint.Interpretation.e_I_e(errorMsg);
		}

		[Pure]
		public static bool InState<TState>(ICRA cra) where TState : StateBase<ICRAEventPoint, CRADataPoint>
		{
			return (cra as AutomatonBase<ICRA, ICRAEventPoint, CRADataPoint>).InState<TState>();
		}
		#endregion
	}
}
